import labmem.Stack.ArrayStack;
import labmem.Stack.LinkedListStack;
import labmem.Stack.Stack;

import java.util.Random;

/**
 * 测试两种栈的性能
 */
public class TestStack {

    //测试使用stack运行opCount个push的pop操作所需要的时间，单位:秒
    private static double testStack(Stack<Integer> stack, int opCount) {

        long startTime = System.nanoTime();

        Random random = new Random();
        for (int i = 0; i < opCount; i++)
            stack.push(random.nextInt(Integer.MAX_VALUE));
        for (int i = 0; i < opCount; i++)
            stack.pop();
        long endTime = System.nanoTime();
        return (endTime - startTime) / 1000000000.0;
    }

    public static void main(String[] args) {
        int opCount = 10000000;

        ArrayStack<Integer> arrayStack = new ArrayStack<Integer>();
        double time1 = testStack(arrayStack, opCount);
        System.out.println("ArrayStack,time: " + time1 + "s");

        LinkedListStack<Integer> linkedListStack = new LinkedListStack<Integer>();
        double time2 = testStack(linkedListStack, opCount);
        System.out.println("LinkedListStack,time: " + time2 + "s");

        //其实这个时间比较很复杂，因为LinkedListStack中包含更多的new操作
    }
}
